package cn.com.ctbri.geomonitor.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.stereotype.Service;

import cn.com.ctbri.geomonitor.bean.UserVO;
import cn.com.ctbri.geomonitor.dao.FlowMapper;
import cn.com.ctbri.geomonitor.dao.FlowNameMapper;
import cn.com.ctbri.geomonitor.model.Flow;
import cn.com.ctbri.geomonitor.model.FlowExample;
import cn.com.ctbri.geomonitor.model.FlowName;
import cn.com.ctbri.geomonitor.model.FlowNameExample;
import cn.com.ctbri.geomonitor.service.UserRestService;

@Service("userRestService")
public class UserRestServiceImpl implements UserRestService {

	@Resource(name = "identityService")
	private IdentityService identityService;

	@Resource(name = "flowMapper")
	private FlowMapper flowMapper;

	@Resource(name = "flowNameMapper")
	private FlowNameMapper flowNameMapper;

	// 用户登录
	@Override
	public User userLogin(String username, String password) {
		User user = this.identityService.createUserQuery().userLastName(username).singleResult();
		if (user == null) {
			return null;
		}
		if (password.equals(user.getPassword())) {
			return user;
		}
		return null;

	}

	// 检测用户是否已经存在
	@Override
	public User checkUserName(String username) {
		User user = this.identityService.createUserQuery().userLastName(username).singleResult();
		return user;
	}

	@Override
	public void register(String username, String password, String role) {
		String uuid = UUID.randomUUID().toString();
		User user = identityService.newUser(uuid);
		user.setLastName(username);
		user.setPassword(password);
		identityService.saveUser(user);

		if (role.equals("enterprise")) {
			// 加入年龄信息
			this.identityService.setUserInfo(user.getId(), "type", "企业");

			identityService.createMembership(user.getId(), "business");
		} else {
			// 加入年龄信息
			this.identityService.setUserInfo(user.getId(), "type", "个人");

			identityService.createMembership(user.getId(), "personal");
		}

	}

	// 查询全部用户
	public List<UserVO> getAllUsers() {
		List<User> users = this.identityService.createUserQuery().list();
		List<UserVO> result = new ArrayList<UserVO>();
		for (int i = 0;i<users.size();i++) {
		

			UserVO userVO = new UserVO();
			userVO.setUserId(users.get(i).getId());
			userVO.setLastName(users.get(i).getLastName());
			// 查询用户组
			Group group = this.identityService.createGroupQuery().groupMember(users.get(i).getId()).singleResult();
			// 查询年龄信息
			//String type = identityService.getUserInfo(user.getId(), "type");
	
			userVO.setGroupName(group.getName());
			userVO.setPasswd(users.get(i).getPassword());
			//userVO.setType(type);
			result.add(userVO);
		
		
		}
		return result;
	}

	// 管理员添加一个用户
	public void saveUser(UserVO userForm) {
		// 生成一个唯一的用户id
		String uuid = UUID.randomUUID().toString();
		User user = this.identityService.newUser(uuid);
		user.setLastName(userForm.getLastName());
		user.setPassword(userForm.getPasswd());
		this.identityService.saveUser(user);
		// 加入用户，类型信息
		this.identityService.setUserInfo(user.getId(), "type", String.valueOf(userForm.getGroupName()));
		// 设置与用户组的关系
		this.identityService.createMembership(user.getId(), userForm.getGroupId());
	}

	@Override
	public List<Flow> getMaterial(String flowId) {
		FlowExample flowExample = new FlowExample();
		flowExample.createCriteria().andFlowIdEqualTo(flowId);
		List<Flow> result = flowMapper.selectByExample(flowExample);
		return result;
	}

	@Override
	public List<FlowName> getAllFlow() {
		FlowNameExample flowNameExample = new FlowNameExample();
		flowNameExample.createCriteria();
		List<FlowName> list = flowNameMapper.selectByExample(flowNameExample);
		return list;
	}

	@Override
	public Group getGroupID(String userId) {
		Group group  =  this.identityService.createGroupQuery().groupMember(userId).singleResult();
		//System.out.println(group.getId());
		return group;
	}

	@Override
	public User getUserByUserId(String userId) {
		User user = identityService.createUserQuery().userId(userId).singleResult();
		return user;
	}

	@Override
	public String updataPassword(String userId, String newPassword) {
		User user = identityService.createUserQuery().userId(userId).singleResult();
		user.setPassword(newPassword);
		identityService.saveUser(user);
		return "sucess";
	}

	@Override
	public String delete(String userId) {
		identityService.deleteUser(userId);
		return "sucess";
	}
	
	
	

	
}
